package huaweiod.first;

import java.util.*;

/**
 * 华为OD机试真题 Java 实现【打印机队列】【2022.11 Q4 新题】
 */
public class A_2023_18_PrintQueue {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        //输入处理
        int count = Integer.parseInt(in.nextLine());
        String[][] operations = new String[count][];
        for (int i = 0; i < count; i++) {
            String[] operation = in.nextLine().split(" ");
            operations[i] = operation;
        }

        // process
        Map<String, PriorityQueue<Integer[]>> map = new HashMap<>();
        for (int i = 1; i <= 5; i++) {
            PriorityQueue<Integer[]> queue = new PriorityQueue<>(new Comparator<Integer[]>() {
                @Override
                public int compare(Integer[] o1, Integer[] o2) {
                    if (o1[0] - o2[0] == 0) {
                        return o1[1] - o2[1];
                    } else {
                        return o2[0] - o1[0];
                    }

                }
            });
            map.put(String.valueOf(i), queue);
        }

        for (int i = 0; i < operations.length; i++) {
            String[] operation = operations[i];
            String p = operation[1];
            PriorityQueue<Integer[]> queue = map.get(p);
            if (operation[0].contains("IN")) {
                String prior = operation[2];
                Integer[] info = new Integer[2];
                info[0] = Integer.valueOf(prior);
                info[1] = i + 1;
                queue.add(info);
            } else {
                Integer[] peek = queue.peek();
                if(peek == null) {
                    System.out.println("NULL");
                } else {
                    System.out.println(peek[1]);
                    queue.poll();
                }
            }
        }
    }
}
